﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using Telerik.Web.UI;
using System.Drawing;

namespace HCFramework.Product
{
    public partial class AdminDefault : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMainTreeData();
            }
        }

        private void BindMainTreeData()
        {
            // Load category
            HCDataContext db = new HCDataContext();
            RadTreeView1.DataSource = db.ProductCategories.OrderBy(ik => ik.OrderDisplay);
            RadTreeView1.DataBind();

            // Load property and Filter
            foreach (RadTreeNode categoryLeafNode in RadTreeView1.GetAllNodes().Where(ik => ik.Nodes.Count < 1))
            {
                int catID = int.Parse(categoryLeafNode.Value);
                ProductCategory cat = C.ProductCategoryController.GetByID(catID);
                // Property
                foreach (ProductProperty property in cat.ProductProperties.OrderBy(ik=>ik.OrderDisplay))
                {
                    RadTreeNode propertyNode = new RadTreeNode("(" + property.Name + ")", property.ProductPropertyID.ToString());
                    propertyNode.Font.Bold = false;
                    categoryLeafNode.Nodes.Add(propertyNode);
                    // Filter
                    foreach (PropertyFilter filter in property.PropertyFilters.OrderBy(ik=>ik.OrderDisplay))
                    {
                        RadTreeNode filterNode = new RadTreeNode("&lt;" + filter.Value + "&gt;", filter.PropertyFilterID.ToString());
                        filterNode.Font.Bold = false;
                        propertyNode.Nodes.Add(filterNode);
                    }
                }
            }

            RadTreeView1.ExpandAllNodes();
        }

        protected void RadTreeView1_ContextMenuItemClick(object sender, RadTreeViewContextMenuEventArgs e)
        {
            if (e.MenuItem.Value == "Delete")
            {
                if (IsFilterNode(e.Node)) C.PropertyFilterController.Delete(int.Parse(e.Node.Value));
                if (IsPropertyNode(e.Node)) C.ProductPropertyController.Delete(int.Parse(e.Node.Value));
                if (IsCategoryNode(e.Node)) C.ProductCategoryController.Delete(int.Parse(e.Node.Value));
                BindMainTreeData();
            }
            if (e.MenuItem.Value == "Manage")
            {
                if(IsPropertyNode(e.Node)) 
                    Response.Redirect("/Product/Admin/FilterManage/Default.aspx?PropertyID=" + e.Node.Value);
                if (IsCategoryNode(e.Node))
                {
                    if(C.ProductCategoryController.HasProperty(int.Parse(e.Node.Value)))
                        Response.Redirect("/Product/Admin/PropertyManage/Default.aspx?CategoryID=" + e.Node.Value);
                    else
                        Response.Redirect("/Product/Admin/CategoryManage/CategoryManage.aspx?ParentID=" + e.Node.Value);
                }
            }
        }

        private bool IsPropertyNode(RadTreeNode radTreeNode)
        {
            string text = radTreeNode.Text;
            return text.StartsWith("(") && text.EndsWith(")");
        }

        private bool IsFilterNode(RadTreeNode radTreeNode)
        {
            string text = radTreeNode.Text;
            return text.StartsWith("&lt;") && text.EndsWith("&gt;");
        }

        private bool IsCategoryNode(RadTreeNode radTreeNode)
        {
            return !IsPropertyNode(radTreeNode) && !IsFilterNode(radTreeNode);
        }
    }
}